/**
 * 
 */
package dfsketches.pathfind;


import static org.junit.Assert.*;

import org.junit.Test;

import dfsketches.bicycles.BicycleStack;

/**
 * @author kislenkor
 *
 */
public class AdvancedPathfindTest {
	@Test(timeout = 100)
	public void findOnePath() {
		int[][] walls = {
				{0, 1, 0, 1},
				{1, 0, 1, 0},
				{1, 1, 1, 0},
				{0, 0, 0, 1}
				};
		BitMap map = new BitMap(walls);
		
		AdvancedPathfind p = new AdvancedPathfind();
		BicycleStack result = p.find(map, 0, 0, 0, 3);
		System.out.println(result.toString());

		assertTrue(result.size() == 7);
		int prev_pos = 0;
		while (!result.isEmpty()) {
			int pos = result.pop();
			if ((
					prev_pos - pos == 1 || prev_pos - pos ==-1 ||
					prev_pos - pos == 4 || prev_pos - pos ==-4 ||
					prev_pos - pos == 5 || prev_pos - pos == 3 ||
					prev_pos - pos ==-3 || prev_pos - pos ==-5 
					) && walls[pos / 4][pos % 4] == 0) {
				prev_pos = pos;
			}
		}
	}

	@Test(timeout = 100)
	public void findFlatPath() {
		int[][] walls = {
				{0, 0, 0, 0},
				{0, 0, 0, 0},
				{0, 0, 0, 0},
				{0, 0, 0, 0}
				};
		BitMap map = new BitMap(walls);
		
		AdvancedPathfind p = new AdvancedPathfind();
		BicycleStack result = p.find(map, 0, 0, 0, 3);
		System.out.println(result.toString());

		assertTrue(result.size() == 3);
		int prev_pos = 0;
		while (!result.isEmpty()) {
			int pos = result.pop();
			if ((
					prev_pos - pos == 1 || prev_pos - pos ==-1 ||
					prev_pos - pos == 4 || prev_pos - pos ==-4 ||
					prev_pos - pos == 5 || prev_pos - pos == 3 ||
					prev_pos - pos ==-3 || prev_pos - pos ==-5 
					) && walls[pos / 4][pos % 4] == 0) {
				prev_pos = pos;
			}
		}
	}

	@Test(timeout = 100)
	public void findComplexPath() {
		int[][] walls = {
				{0, 0, 1, 0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0, 1, 0, 0},
				{1, 1, 1, 0, 0, 1, 0, 0},
				{0, 0, 1, 0, 1, 1, 1, 0},
				{0, 1, 1, 0, 1, 0, 0, 0},
				{0, 0, 0, 0, 0, 0, 0, 0},
				{0, 1, 1, 1, 1, 0, 0, 0},
				{0, 0, 0, 0, 1, 0, 0, 0}
				};
		BitMap map = new BitMap(walls);
		
		AdvancedPathfind p = new AdvancedPathfind();
		BicycleStack result = p.find(map, 0, 0, 1, 3);
		System.out.println(result.toString());

		assertTrue(result.size() == 9);
		int prev_pos = 0;
		while (!result.isEmpty()) {
			int pos = result.pop();
			if ((
					prev_pos - pos == 1 || prev_pos - pos ==-1 ||
					prev_pos - pos == 4 || prev_pos - pos ==-4 ||
					prev_pos - pos == 5 || prev_pos - pos == 3 ||
					prev_pos - pos ==-3 || prev_pos - pos ==-5 
					) && walls[pos / 4][pos % 4] == 0) {
				prev_pos = pos;
			}
		}
	}

}